//
//  MySettingController.m
//  FD
//
//  Created by Yasin on 2018/10/16.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  设置

#import "MySettingController.h"
#import "NewModifyPwdController.h" //修改登录密码

#import "LoginAndRegisterController.h" //登陆注册页面 xib
#import "FDTabBarController.h" //2019-2-15解决点击跳过导致的bug



#import "FDUserTool.h"
#import "FDPushWebSocketTool.h" //推送消息websocket

//view自定义弹框
#import "XLAlertView.h" //1-20

@interface MySettingController ()<UITableViewDelegate,UITableViewDataSource>
/**tableView*/
@property (nonatomic,  strong) UITableView *tv;
/**表尾*/
@property (nonatomic,  strong) UIView *footerView;
@end

@implementation MySettingController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    self.navigationItem.title = NSLocalizedString(My_setting, @"设置");
    self.view.backgroundColor = kMainLightGrayColor;
    self.tv.frame = self.view.bounds;
    [self.view addSubview:self.tv];

    self.tv.tableFooterView =  self.footerView;
    self.tv.scrollEnabled = NO;
//    //取消导航条线
//    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
//    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
//    [self.navigationController.navigationBar setTranslucent:NO];
}


- (UITableView *)tv {
    if (!_tv) {
        _tv = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        //代理
        _tv.delegate = self;
        _tv.dataSource = self;
        //注册cell
        [_tv registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
        _tv.backgroundColor = kMainLightGrayColor;
        _tv.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tv;
}

- (UIView *)footerView {
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
        _footerView.backgroundColor = kMainLightGrayColor;
        
        //退出登录按钮
        UIButton *logoutBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        logoutBtn.frame = CGRectMake(20, 100, SCREEN_WIDTH - 40, 40);
        //国际化词条
        NSString *logoutStr = NSLocalizedString(LoginOut, @"退出登录");
        [logoutBtn setTitle:logoutStr forState:(UIControlStateNormal)];
        [logoutBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        logoutBtn.backgroundColor = kMainRedColor;
        logoutBtn.layer.cornerRadius = 5;
        logoutBtn.layer.masksToBounds = YES;
        [logoutBtn addTarget:self action:@selector(logoutBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [_footerView addSubview:logoutBtn];
    }
    return _footerView;
}
- (void)logoutBtnAction:(UIButton *)btn {
    [self showAlertView]; //弹框
}

- (void)showAlertView {
    NSString *titleStr = NSLocalizedString(LoginOut, @"退出登录");
    NSString *msgStr =  NSLocalizedString(SureLogOutCurrentAccout, @"是否退出当前账号？");
    NSString *cancelStr = NSLocalizedString(Cancel, @"取消");
    NSString *sure = NSLocalizedString(Sure, @"确认");
    
   XLAlertView *xlAlertV = [[XLAlertView alloc] initWithTitle:titleStr message:msgStr sureBtn:sure cancleBtn:cancelStr];
    
    xlAlertV.resultIndex = ^(NSInteger index) {
        FDLog(@"index:%ld",(long)index);
        if (index == 1) { //2019-2-25取消按钮不做操作
            return ;
        }
        
        //给一个弱提示.
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        
        CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
        
        //退出登录接口
        [FDHttpAPi requestLogoutSuccessBlock:^(id responseObjc) {
            
            [hud hideAnimated:YES];
            
            NSString *event = responseObjc[@"event"];
            if ([event isEqualToString:SUCCESS]) {
                
                //断开推送websocket连接
                [[FDPushWebSocketTool defaultFdWebPushSockcet] closePushWebSocket];
                
                [self skipTologinVC];
            }else {
                [JRToast showWithText:responseObjc[describe]];
            }
        } failBlock:^(NSError *error) {
            [hud hideAnimated:YES];
            
            CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
            FDLog(@"%@ 耗时:%f",[error localizedDescription],(end - start));
        }];
    };
    [xlAlertV showXLAlertView]; //显示弹框
}


//退出登录
- (void)skipTologinVC {

    //清空导航栈 不要动画
    [self.navigationController popToRootViewControllerAnimated:NO];
    
    //清空本地数据
    [FDUserTool fd_clearUserInfo]; //2019-2-25要保存密码，∴不清空


    //切换根视图控制器
    LoginAndRegisterController *loginVC = [[LoginAndRegisterController alloc] initWithNibName:@"LoginAndRegisterController" bundle:nil];
    UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:loginVC];
    [UIApplication sharedApplication].keyWindow.rootViewController = navVC;
    
    [self changeFDTarbarVCSelectIndex0];
}

////2019-2-15解决点击跳过导致的bug,选中首页. 不需要切换根视图控制器
- (void)changeFDTarbarVCSelectIndex0 {
    FDTabBarController *fdtabbarVC = [FDTabBarController shareFDTabarVC];
    fdtabbarVC.selectedIndex = 0;
}



#pragma mark - 代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
//线
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *v = [UIView new];
    v.backgroundColor = kMainLightGrayColor;
    return v;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell   *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //>
    cell.textLabel.text = NSLocalizedString(SetLoginPwd, @"登录密码");
    cell.textLabel.font = [UIFont systemFontOfSize:15.0];
    
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        NewModifyPwdController *modifyVC = [[NewModifyPwdController alloc] init];
        [self.navigationController pushViewController:modifyVC animated:YES];
    }
}



@end
